<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String _id=request.getParameter("_");
%>
<form id="form_dictitme_input" class="form-horizontal">
    <input type="hidden" name="keyId" id="keyId" value="${id}"/>
    <input type="hidden" name="_dictId" id="_dictId" value="${dictId}"/>
    <div id="container_dictitme_input"></div>
</form>

<script id="templates_dictitme_input" type="text/x-jsrender">

    <div class="form-group">
        <label class="col-md-3 control-label">系统字典</label>
        <div class="col-md-7">
            <input type="hidden" name="dictId" id="dictId" value="{{:dictId}}"/>
            <input type="text" name="dictName" id="dictName" value="{{:dictName}}" class="form-control" readonly>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">字典项名称</label>
        <div class="col-md-7">
            <input type="text" name="name" id="name" value="{{:name}}" class="form-control" placeholder="请输入字典项名称">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">关键字</label>
        <div class="col-md-7">
            <input type="text" name="value" id="value" value="{{:value}}" class="form-control" placeholder="请输入关键字">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">排序号</label>
        <div class="col-md-7">
            <input type="text" name="sortNo" id="sortNo" value="{{:sortNo}}" class="form-control" placeholder="请输入排序号">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">描述信息</label>
        <div class="col-md-7">
            <textarea name="description" id="description" rows="3" class="form-control" placeholder="请输入描述信息">{{:description}}</textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-7">
	        <div class="checkbox">
	            <input type="checkbox" name="ifKey" id="ifKey" class="magic-checkbox" value="ifKey">
		        <label for="ifKey">允许禁用</label>
	        </div>
		    <small class="help-block">注: 用户级系统字典配置是否允许禁用</small>
		</div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-7">
	        <div class="checkbox">
	            <input type="checkbox" name="ifSelectable" id="ifSelectable" class="magic-checkbox" value="ifSelectable">
		        <label for="ifSelectable">表单可选</label>
	        </div>
		    <small class="help-block">注: 业务表单中是否允许选择</small>
		</div>
    </div>

</script>

<script>
    $(function(){

        // 加载数据
        fn_dictitem_input_load();

    });

    function fn_dictitem_input_load() {
        var $templates = $.templates("#templates_dictitme_input");
        var $container = $("#container_dictitme_input");
        var keyId = $("#keyId").val();
        var dictId = $('#_dictId').val();

        var url;
        var data;
        if (keyId) {
            url = "<%=path%>/main/dictitem/read/" + keyId;
        } else {
            url = "<%=path%>/main/dictitem/init";
            if (dictId) {
                data = {dictId: dictId};
            }
        }
        __ajax_get(url, data, function (data) {
            if (data.state == 1) {
                var entity = data.data;

                // 初始化模板
                var html = $templates.render(entity);
                $container.html(html);

                // 表单数据校验
                fn_dictitem_input_validate();

                $('#ifKey').attr('checked', entity.ifKey == 0);
                $('#ifSelectable').attr('checked', entity.ifSelectable == 0);
            }
        });
    }

    // 表单数据校验
    function fn_dictitem_input_validate() {
        __init_validate('form_dictitme_input',{
            rules : {
                name : {
                    required : true
                }

            },
            messages : {
                name : {
                    required : "请输入字典项名称"
                }
            }
        });
    }

    function fn_dictitem_input_save(callback) {
        if (!$("#form_dictitme_input").valid()) {
           return false;
        }
        __confirm_dialog(null, '确定提交么?',
                function () {
                    __form_save('form_dictitme_input', '<%=path%>/main/dictitem/save', function(data) {
                        if(data.state == 1) {
                            callback(data.data);
                        }else {
                            callback();
                        }
                    });
                },
                function () {
                    callback();
                }
        );
    }

</script>